/**
 * 富客户端提交方式
 * Created by oscar-yang on 2016/1/27.
 */
angular.module('services.rpc', [])
.factory('rpc', ['$http', '$q', function ($http, $q) {
    var result = {

        // 保存参数
        params : {}

        /*
        * 设置参数
        */
        , setParam : function (paramName, paramValue) {
            var args = arguments;
            if (args.length < 2) {
                this.params = paramName;
            } else {
                this.params[paramName] = paramValue;
            }
            return result;
        }

        /*
        * post方式提交，传统的方式
        * @example
        * var url = '/haha'
        *     , data = {
        *           'username' : username
        *           , 'password' : password
        *       }
        * rpc.post(url, data);
        */
        , post : function (url, setting) {
            var delay = $q.defer()
                , _setting = {};

            // 设置参数
            var data = $.param(this.params);

            angular.extend(_setting, setting);
            _setting.headers = {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'};
            $http.post(url, data, _setting)
                .success(function (response) {
                    delay.resolve(response);
                }).error(function (error) {
                    delay.reject(error);
                });
            return delay.promise;
        }

        /**
         * get方式提交
         */
        , get : function () {}

        /**
         * 文件上传提交
         */
        , upload : function () {}
    };

   return result;
}]);
